<template>
	<view>
		<cu-custom bgColor="bg-blue-11" :isBack="true">
			<view slot="backText">
				返回
			</view>
			<view slot="content">
				登录
			</view>
		</cu-custom>
		<view class="margin padding-sm bg-white" style="border-radius:20rpx;">
			<view class="cu-card case no-card">
				<view class="cu-item shadow">
					<view class="image" style='padding:30rpx 30rpx;'>
						<image src="../../static/bg_img/zhxy.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
			<view class="margin padding-sm">
				<view class="cu-form-group text-right">
					<view class="title">账号</view>
					<input placeholder="学号/工号" v-model="form.username"></input>
					<text class="cuIcon-myfill text-blue"></text>
				</view>
				<view class="cu-form-group text-right">
					<view class="title">密码</view>
					<input placeholder="密码" type='password' v-model="pwdBeforeEncode"></input>
					<text class="cuIcon-emojifill text-orange"></text>
				</view>
			</view>
			<view v-if="captcha_path" class="margin padding-sm">
				<view class="cu-form-group text-right">
					<image :src="captcha_path" style="width:120px;height:80px;" mode='aspectFit' @click="getCode()">
					</image>
					<input placeholder="验证码" v-model='form.code'></input>
				</view>
			</view>
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-orange-1 round shadow-blur lg" open-type="getUserInfo"
					@click="login">统一认证登录</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		req,
		reqJson,
		rootUrl
	} from '../../utils/util.js'
	import { encrypt } from '../../utils/rsaEncrypt.js'
	export default {
		created() {
			this.getCode()
			this.getOpenId()
		},
		data() {
			return {
				form: {
					username: '17220001',
					password: '',
					uuid: '',
					openid: '',
					code:''
				},
				cookies: "",
				pwdBeforeEncode:"",
				captcha_path: "",
				captcha: '',
				showDialog: false,
				res: {}
			}
		},
		methods: {
			getOpenId() {
				//漫路h
				uni.login({
					success: res => {
						console.info("tempcode-->for get openid");
						//code值(5分钟失效)
						console.info(res.code);
						//小程序appid
						let appid = 'wxd3c2897ed65e2129'; //我瞎写的
						//小程序secret
						let secret = '701edc91c79f5f0fd9b27c236e1d1ff2'; //我瞎写的
						//wx接口路径
						let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' +
							secret + '&js_code=' + res.code + '&grant_type=authorization_code';
						uni.request({
							url: url, // 请求路径
							method: 'GET', //请求方式
							success: result => {
								console.info("get openid");
								//响应成功
								//这里就获取到了openid了
								this.form.openid = result.data.openid
								uni.setStorage({
									key: 'user',
									data: result.data.openid
								})
							},
							fail: err => {
								uni.showModal({
									content: err.message,
									showCancel: false
								})
							} //失败
						});
					}
				});
			},
			getCode() {
				const uuid = this.form.uuid ? this.form.uuid : ''
				const params = {
					uuid: uuid
				}
				this.$req("auth/code", "get", params, res => {
					this.captcha_path = res.img
					this.form.uuid = res.uuid;
				})
			},
			login() {
				if (!this.form.username || !this.pwdBeforeEncode) {
					uni.showModal({
						content: "请输入账号或密码！",
						showCancel: false
					})
				} else {
					// 查看是否授权
					uni.getSetting({
						success: res => {
							// if (res.authSetting['scope.userInfo']) 
							if (res.authSetting['scope.userInfo']) {
								// 已经授权，可以直接调用 getUserProfile 获取头像昵称
								uni.getUserProfile({
									desc:'登录',
									success: res => {
										console.log("getWxInfo")
										//性别 0：未知、1：男、2：女
										uni.setStorageSync('wx_info', res.userInfo)
									}
								})
								this.form.password=encrypt(this.pwdBeforeEncode)
								let postData = this.form
								if (this.cookies) {
									postData['cookies'] = this.cookies
									postData['captcha'] = this.captcha
								}
								this.$reqJson("wxapi/auth/login", "post", postData, res => {
									if (res.user) {
										let user_info = res.user.user
										user_info['password'] = this.form.password
										uni.setStorageSync('user_info', user_info)
										uni.navigateBack(-1)
									}else{
										uni.showModal({
											content: res.message,
											showCancel: false
										})
										this.getCode()
									}
								})
							} else {
								uni.showModal({
									content: "请允许获得您的公开信息",
									showCancel: false
								})
							}
						},
						fail: err => {
							uni.showModal({
								content: err,
								showCancel: false
							})
						}
					})
				}
			}
		}
	}
</script>

<style>

</style>
